package org.hlopik.riverGuide.client;

import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.hlopik.riverGuide.client.controller.AddRiverGuideCommand;
import org.hlopik.riverGuide.client.controller.ListRiverGuidesCommand;
import org.hlopik.riverGuide.client.controller.StartupCommand;
import org.hlopik.riverGuide.client.view.AddRiverGuideFormMediator;
import org.hlopik.riverGuide.client.view.RiverGuideListMediator;
import org.puremvc.java.multicore.interfaces.IMediator;
import org.puremvc.java.multicore.patterns.facade.Facade;

/**
 *
 * @author f
 */
public class ApplicationFacade extends Facade {

    public static final String NAME = "ApplicationFacade";
    private static ApplicationFacade instance = null;
    public static final String STARTUP = "startup";
    public static final String ADD_RIVER_GUIDE = "addRiverGuide";
    public static final String ADD_RIVER_GUIDE_SUCCESS = "addRiverGuideSuccess";
    public static final String ADD_RIVER_GUIDE_FAULT = "addRiverGuideFault";
    public static final String GET_RIVER_GUIDES = "listRiverGuides";
    public static final String GET_RIVER_GUIDES_SUCCESS = "getRiverGuidesSuccess";
    public static final String GET_RIVER_GUIDES_FAULT = "getRiverGuidesFault";

    /**
     * Constructor.
     */
    protected ApplicationFacade() {
        super(NAME);
    }

    /**
     * get the instance.
     * @return the singleton
     */
    public static ApplicationFacade getInstance() {
        if (instance == null) {
            instance = new ApplicationFacade();
            startup(instance);
        }
        return instance;
    }

    /**
     * Start the application
     * @param facade the facade
     */
    public static void startup(Facade facade) {
        facade.sendNotification(STARTUP);
    }

    /**
     * Initialize controller. Register the commands.
     */
    @Override
    protected void initializeController() {
        super.initializeController();
        registerCommand(STARTUP, new StartupCommand());
        registerCommand(ADD_RIVER_GUIDE, new AddRiverGuideCommand());
        registerCommand(GET_RIVER_GUIDES, new ListRiverGuidesCommand());
    }

    /**
     * Attach the view to the root panel.
     * @param panel the panel
     */
    public void showIn(Panel panel) {
        IMediator mediator1 = retrieveMediator(RiverGuideListMediator.NAME);
        IMediator mediator2 = retrieveMediator(AddRiverGuideFormMediator.NAME);
        panel.clear();
        VerticalPanel vp = new VerticalPanel();
        vp.add((Widget) mediator2.getViewComponent());
        vp.add((Widget) mediator1.getViewComponent());
        panel.add(vp);
    }
}
